package com.ycsj.goldmedalnewconcept.teacher.adapter;

import android.content.Context;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.ycsj.common.base.BusInfo;
import com.ycsj.common.view.dialog.AlertDialogUtil;
import com.ycsj.common.view.scroll.MyListView;
import com.ycsj.goldmedalnewconcept.R;
import com.ycsj.goldmedalnewconcept.common.activity.SingleQuestionDetailsActivity;
import com.ycsj.goldmedalnewconcept.common.bean.OptionInfo;
import com.ycsj.goldmedalnewconcept.common.bean.SubListBean;
import com.ycsj.goldmedalnewconcept.common.bean.TkItemBean;
import com.ycsj.goldmedalnewconcept.teacher.activity.TeacherTKAssignHomeworkActivity;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

/**
 * 题目列表的适配器
 * Created by Kevin on 2019/3/8
 */
public class AssignQuestionRvAdapter extends BaseQuickAdapter<TkItemBean, BaseViewHolder> {

    private Context context;
    private int type;
    private String tvConent;
    private String temp = "";
    //字母索引
    private String[] indexs = {"A", "B", "C", "D", "E", "F", "G"};

    public AssignQuestionRvAdapter(Context context, int layoutResId, @Nullable List<TkItemBean> data,int type) {
        super(layoutResId, data);
        this.context = context;
        this.type = type;
    }

    @Override
    protected void convert(final BaseViewHolder helper, final TkItemBean item) {

        //题干
        TextView tv = helper.getView(R.id.tv_question);
        //题目类型和题干,题目类型需要进行变色
        TextView tv_type = helper.getView(R.id.tv_question_type);

        //删除题目
        RelativeLayout iv_del = helper.getView(R.id.iv_del);

        //隐藏删除按钮 1预览隐藏
        if(type==1){
            iv_del.setVisibility(View.GONE);
        }else{
            iv_del.setVisibility(View.VISIBLE);
        }

        //整个外层布局
        LinearLayout ll_out = helper.getView(R.id.ll_out);
        ll_out.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // 跳转到每个题型的详情界面
                SingleQuestionDetailsActivity.goActivity(context, item, 3 + "");
            }
        });

        //拼接题目类型和题干
        if(TextUtils.isEmpty(item.getN_topic_name())){
            tvConent = "(" + item.getN_type_name() +")";
        }else{
            tvConent = "(" + item.getN_type_name() +"-"+item.getN_topic_name()+ ")";
        }

        if ("6".equals(item.getTypeid())) {

            for (int i=0;i<item.getSub_list().size();i++){
                if(i==0){
                    temp = "\n"+(i+1)+"."+item.getSub_list().get(i).getTitle()+"\n";
                }else{
                    temp += (i+1)+"."+item.getSub_list().get(i).getTitle()+"\n";
                }
            }
        }
        tv_type.setText(tvConent);
        tv.setText(item.getContent()+temp);

        //答案选项的ListView
        MyListView lv = helper.getView(R.id.lv);
        lv.setVisibility(View.GONE);

        if (!"6".equals(item.getTypeid())) {

            lv.setVisibility(View.GONE);

        } else {


            //判断是否是有选项的题目
            SubListBean subListBean = item.getSub_list().get(0);

            if (null != subListBean) {
                List<String> list = subListBean.getList();

                if (null == subListBean.getList() || isEmpty(list)) {
                    //返回的集合为null或者集合里面所有选项都是"",证明没有ABC选项
                    lv.setVisibility(View.GONE);
                } else {
                    lv.setVisibility(View.VISIBLE);
                    List<OptionInfo> optionInfos = new ArrayList<>();
                    for (int i = 0; i < list.size(); i++) {
                        OptionInfo optionInfo = new OptionInfo();
                        optionInfo.optionIndex = indexs[i];
                        optionInfo.optionContent = list.get(i);
                        optionInfos.add(optionInfo);
                    }

                    ShowAnswerAdapter adapter = new ShowAnswerAdapter(context, optionInfos, R.layout.item_lv_answer);
                    lv.setAdapter(adapter);

                    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                        @Override
                        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                            SingleQuestionDetailsActivity.goActivity(context, item,"3");
                        }
                    });
                }
            }
        }


        iv_del.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialogUtil.showAlertDialog(true, context, "是否确认删除该题目？","确定删除", new AlertDialogUtil.onListener() {
                    @Override
                    public void onClickOk() {
                        BusInfo busInfo = new BusInfo();
                        busInfo.type = TeacherTKAssignHomeworkActivity.TKType;
                        busInfo.questionPosition = helper.getPosition();
                        EventBus.getDefault().post(busInfo);
                    }

                    @Override
                    public void onClickDismiss() {
                    }
                });
            }
        });
    }


    /**
     * 判断集合里面是否全是空串的方法
     *
     * @param contents
     * @return
     */
    public boolean isEmpty(List<String> contents) {
        boolean res = true;
        for (int i = 0; i < contents.size(); i++) {
            if (!TextUtils.isEmpty(contents.get(i))) {
                res = false;
            }
        }

        return res;
    }
}
